using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using I2.Loc;
using UnityEngine;

public class TeamGameMode : DefaultGameMode
{
	private int count;

	public override bool CanStartNow()
	{
		int countPlayerForStart = PVPManager.instance.CountPlayerForStart;
		bool flag = GameController.instance.listPlayers.Count((PlayerBehavior p) => p.localTeam == PunTeams.Team.blue && p.photonView.owner.GetValueByKey(PlayerPropsKeys.startPVP, false)) >= countPlayerForStart / 2;
		bool flag2 = GameController.instance.listPlayers.Count((PlayerBehavior p) => p.localTeam == PunTeams.Team.red && p.photonView.owner.GetValueByKey(PlayerPropsKeys.startPVP, false)) >= countPlayerForStart / 2;
		return flag && flag2;
	}

	public override void StartRound()
	{
		base.StartRound();
		CoroutineRunner.Instance.StartCoroutine(SwitchMasterInSeconds(7f));
		PVPManager.SetCancelEndTdm(10f);
	}

	public override void RestartRound()
	{
		base.RestartRound();
		CoroutineRunner.Instance.StartCoroutine(SwitchMasterInSeconds(7f));
		PVPManager.SetCancelEndTdm(10f);
	}

	private IEnumerator SwitchMasterInSeconds(float seconds)
	{
		yield return new WaitForSeconds(seconds);
		if (TDMController.instance == null || count > 4)
		{
			yield break;
		}
		if (TDMController.myTeam == PunTeams.Team.none)
		{
			count++;
			if (PhotonNetwork.connected)
			{
				PhotonNetwork.SetMasterClient(PhotonNetwork.masterClient.GetNext());
				CoroutineRunner.Instance.StartCoroutine(SwitchMasterInSeconds(7f));
			}
		}
		else
		{
			count = 0;
		}
	}

	public override string EndGameModeTitle()
	{
		return ScriptLocalization.Get("Game_Common/coop_over");
	}

	public override TDMResult GetRoundResult()
	{
		PunTeams.Team myTeam = TDMController.myTeam;
		PunTeams.Team enemyTeam = TDMController.enemyTeam;
		int num = 0;
		int num2 = 0;
		if (PhotonNetwork.room == null)
		{
			return TDMResult.Draw;
		}
		if (PhotonNetwork.room == null)
		{
			return TDMResult.Draw;
		}
		if (PhotonNetwork.room.customProperties.ContainsKey(myTeam.ToString()))
		{
			num = (int)PhotonNetwork.room.customProperties[myTeam.ToString()];
		}
		if (PhotonNetwork.room.customProperties.ContainsKey(enemyTeam.ToString()))
		{
			num2 = (int)PhotonNetwork.room.customProperties[enemyTeam.ToString()];
		}
		if (num > num2)
		{
			return TDMResult.Winner;
		}
		if (num2 > num)
		{
			return TDMResult.Loser;
		}
		return TDMResult.Draw;
	}

	public override List<PlayersInfoScroll.PlayerInfoPanelParams> GetPlayersList<T>(Func<T, bool> filter)
	{
		List<PlayersInfoScroll.PlayerInfoPanelParams> list = new List<PlayersInfoScroll.PlayerInfoPanelParams>();
		List<PlayerBehavior> source = GameController.instance.listPlayers;
		if (filter != null)
		{
			source = source.Where((PlayerBehavior x) => filter(x as T)).ToList();
		}
		List<PhotonPlayer> source2 = source.Select((PlayerBehavior p) => p.photonView.owner).ToList();
		source2 = source2.Where((PhotonPlayer x) => x != null).ToList();
		Dictionary<int, int> dictionary = new Dictionary<int, int>();
		if (BCWFlagManager.instance != null && BCWFlagManager.instance.winerTeam != 0)
		{
			dictionary = BCWFlagManager.instance.GetScores();
		}
		TeamExtensions.BubbleSort(source2, TeamExtensions.SortPhotonPlayerForCaptureTheFlag);
		int num = 0;
		PhotonPlayer player;
		foreach (PhotonPlayer item in source2)
		{
			player = item;
			Color magenta = Color.magenta;
			Color magenta2 = Color.magenta;
			if (player != null)
			{
				if (player.isLocal)
				{
					magenta = GameInfoWindow.instance.myPlayerBG;
					magenta2 = GameInfoWindow.instance.myPlayerText;
					PVPManager.instance.myPlace = num + 1;
				}
				else
				{
					magenta = new Color(27f / 85f, 0.38039216f, 0.40784314f, 1f);
					magenta2 = Color.white;
				}
				int flagScore = ((!dictionary.ContainsKey(player.ID)) ? (-1) : dictionary[player.ID]);
				int num2 = -1;
				int num3 = GameController.instance.listPlayers.FindIndex((PlayerBehavior x) => x.photonView.owner == player);
				if (num3 >= 0)
				{
					num2 = GameController.instance.listPlayers[num3].Points;
				}
				list.Add(new PlayersInfoScroll.PlayerInfoPanelParams());
				list[num].place = num + 1;
				list[num].name = player.GetValueByKey(PlayerPropsKeys.nickname, "Empty name in Props");
				list[num].state1 = string.Empty;
				list[num].state2 = num2.ToString();
				list[num].flagScore = flagScore;
				list[num].bgColor = magenta;
				list[num].textColor = magenta2;
				list[num].playerId = player.GetValueByKey(PlayerPropsKeys.playerId, string.Empty);
				num++;
			}
		}
		return list;
	}

	public override PlayersScrollType TypeOfScrollForGameMode()
	{
		return PlayersScrollType.GeneralTeamMatch;
	}
}
